﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.Admin.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.Admin.Service;

namespace CashFlow.Admin.Service.Implement
{
    public class OrderService : Service<OrderEntity>, IOrderService
    {
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }

        public IList<OrderAndUserInfo> QueryAllChildUsersOrder(string date)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            return QueryAllChildUsersOrder(ui.ShopManagerUserId, date);
        }

        public IList<OrderAndUserInfo> QueryAllChildUsersOrder(int userId
            , string date)
        {
            var userService = GetUserService();
            var users = userService.QueryAllChildUsersByUserId(userId);
            var userIds = users.Select(x => x.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                    .InnerJoin<ShopInfoEntity>((o, u) => o.BuyerUserId == u.Id)
                    .Where((o, u) => o.CreateTime.ToString().Substring(0, 10) == date
                        && userIds.Contains(u.Id))
                    .Select((o, u) => new OrderAndUserInfo
                    {
                        OrderInfo = o,
                        UserInfo = u
                    }).ToList();
                return ls;
            }
        }

        public IList<OrderBuySellInfo> QueryShopAllUsersOrder(int userId
            , string orderNo
            , string date
            , int? state
            , string buyer
            , string seller)
        {
            var userService = GetUserService();
            var users = userService.QueryAllChildUsersWithSameShopByUserId(userId);
            var userIds = users.Select(x => x.UserInfo.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<OrderEntity>()
                            .InnerJoin<UserInfoEntity>((o, b) => o.BuyerUserId == b.Id)
                            .InnerJoin<UserInfoEntity>((o, b, s) => o.SellerUserId == s.Id)
                            .Where((o, b, s) => userIds.Contains(o.BuyerUserId));
                if (!string.IsNullOrEmpty(orderNo))
                {
                    query = query.Where((o, b, s) => o.OrderNo.Contains(orderNo));
                }
                if (!string.IsNullOrEmpty(date))
                {
                    query = query.Where((o, b, s) => o.CreateTime.ToString().Substring(0, 10) == date);
                }
                if (state.HasValue)
                {
                    query = query.Where((o, b, s) => o.State == state);
                }
                if (!string.IsNullOrEmpty(buyer))
                {
                    query = query.Where((o, b, s) => b.Name.Contains(buyer));
                }
                if (!string.IsNullOrEmpty(seller))
                {
                    query = query.Where((o, b, s) => s.Name.Contains(seller));
                }

                var ls = query.Select((o, b, s) => new OrderBuySellInfo
                {
                    Order = o,
                    Buyer = b,
                    Seller = s
                }).ToList();
                return ls;
            }
        }

        public IList<OrderBuySellInfo> QueryOrders(int userId, string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                    .InnerJoin<UserInfoEntity>((o, u1) => o.BuyerUserId == u1.Id)
                    .InnerJoin<UserInfoEntity>((o, u1, u2) => o.SellerUserId == u2.Id)
                    .Where((o, u1, u2) => o.CreateTime.ToString().Substring(0, 10) == date
                        && (u1.Id == userId || u2.Id == userId))
                    .Select((o, u1, u2) => new OrderBuySellInfo
                    {
                        Order = o,
                        Buyer = u1,
                        Seller = u2
                    })
                    .ToList();

                return ls;
            }
        }

        public IList<OrderBuySellInfo> QueryOrders(string date)
        {
            var service = GetAdminService();
            var ui = service.GetLoginUserInfo();
            return QueryOrders(ui.ShopManagerUserId, date);
        }

        public IList<OrderEntity> QueryAllOrders(string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                    .Where(x => x.CreateTime.ToString().Substring(0, 10) == date)
                    .ToList();
                return ls;
            }
        }
    }
}
